/****
Program for: Conscription and Military Service: Do They Result in Violent 
and Non-Violent Incarcerations and Recidivism?

This program computes the bounds on LNATE, LMATE, ATT, ATE, LATE 
1948-1952 white and nonwhite in the pooled survey year.

02/14/2019
***/ 
capture log close
***Install package: Distinct 
ssc inst distinct

*****Please replace here with the directory folder
cd "C:\Users\xinto\Dropbox\Vietnam Veteran Paper\Crime Paper\2020RevisionDataSet\Bootstrap Folder\1951 birth cohort"

set more off
clear all
set memory 350m
set matsize 1000

program drop _all

global outcomes = "Evernonviolent"
global race4852= "0 1"

use pop_sample_03162020, clear 

keep weight yob Census birthYear white study id total_male_inmate /// 
birthMonth birthDay lott totalSample currentViolent nonviolent nonviolent_new vv eligible /// 
contactJuvenile violent_desistance nonviolent_desistance violent_r_draft nonviolent_r_draft ///
violent_drug_draft nonviolent_drug_draft ygroup* birth monthPop drawingYear APNheight ///
seasonYear seasonPop seasonYear_group seasonYear_group* monthYear_group monthYear_group* inmate /// 
fe_yob* fe_mob* fe_yobmob* yobmob lindo criminal violent nonviolent /// 
mob dob birth ygroup* fe_yob* yobmob fe_mob* drawingYear induction yobRSNID ///
relativeLotteryNumber relLotNumInteract relLotNumSquared relLotNumSqInter yeargroup ///
relLotNumCubed  relLotNumCuInter staylength yob lotteryNumber white study surveyyear LScurrentViolent LSnonviolent ///
LSviolent_desistance LSnonviolent_desistance LSviolent_r_draft LSnonviolent_r_draft LSviolent_drug_draft LSnonviolent_drug_draft ///
EvercurrentViolent Evernonviolent Everviolent_desistance Evernonviolent_desistance Everviolent_r_draft Evernonviolent_r_draft ///
 Everviolent_drug_draft Evernonviolent_drug_draft survey1 survey2 survey3 

qui do I2_pop_test_be_0804
qui do I2_pop_test_racial_be_0804

log using 1951_offense_log, text replace


/* Test 
gen cohort_sample=. 

   *Note that the cohort_sample also mark out the VSUS records 
   replace cohort_sample=1 if ((white==1 & inmate==0 & ygroup1==1 ) /// 
   |( white==1 & totalSample==1 & ygroup1==1 & currentViolent!=9 & inmate==1 & Census==0))

   replace cohort_sample=0 if cohort_sample==. & (Census==0|inmate==0) 
   
   ****Step 3: Create birth_cohort variable to mark out the corresponding birth cohort
   gen birth_cohort=.
   replace birth_cohort=1 if white==1 & ygroup1==1 & (inmate==0 | Census==0) 
   replace birth_cohort=0 if birth_cohort==. & (inmate==0 | Census==0) 
   
   ****Step 4: Drop the observation of the SIPP data as the population estimates are obtained from other sources  
   drop if Census==1 

   ***Step 5: svy_male_inmate is the variable with the values of the total inmates incarcerated in state prison-
   * -in 1979, 1986, and 1991, and in the federal prison in 1991 respectively 
   gen svy_male_inmate=total_male_inmate if Census==0 

   gen T=vv
   gen Z=eligible
   ***Step 6: Y is the binary indicator to compute the proportion of inmates conditional on veteran status and draft-eligibilities-
   * with I2_pop_survey_be
   gen Y=currentViolent*cohort_sample
   replace Y=0 if Y==. & (Census==0|inmate==0)
   gen W=weight 
   
   ***Population estimates 
   **Strata proportion 
   
  
   global race 1
   
   di "input the proportion of the white sample"
   sca Cc=0.1425
   sca li Cc 
   
   sca Cat=0.2337
   sca li Cat 
   
   sca Cnt=0.6238
   sca li Cnt 
   
   **Probabilities 
   sca p10=Cat
   sca p01=Cnt
   sca p11=Cat+Cc
   sca p00=Cnt+Cc
   sca li p10
   sca li p01
   sca li p11 
   sca li p00 
   
   **Conditional means of veterans and eligible 
   sca PrT1=0.2945
   sca li PrT1
   sca PrT0=1-0.2945
   sca li PrT0
   
   sca PrZ1=0.4265
   sca li PrZ1 
   sca PrZ0=1-0.4265
   sca li PrZ0
   
   global tsls_outcome "sviolent"
   
   *Set the yeargroup to for the lindo estimates 
   global lindo_yeargroup 1
   
   di "$tsls_outcome"
   di "$race"
   di "$lindo_yeargroup"
   
   I2_pop_racial_be 

*/

foreach crime_outcome in $outcomes{

global outcome1="`crime_outcome'"

I2_pop_test_be

*Step 8: bootstrapping (uncomment to run the bootstrap)
   bootstrap _b, r(2500) str(Census) saving(vc_`crime_outcome'_ygroup4_white_nonwhite_test_I2_be.dta, double replace) seed(1989): I2_pop_test_be 
   display("program finished running")
   
   }
   
log close   
